Repository navigation
driver/redis: bound the connection handshake by DialTimeout (v1.6.0, Refs #1205) - #1209
Merged
Merged
Conversation
…Refs #1205) A node that accepts TCP and never answers HELLO holds the dial, and so Client.Subscribe, ClusterClient.Subscribe/SSubscribe and any Do that needs a new conn, until the caller's context ends: for ever under context.Background. These tests fail on main (all three dial paths, Client and ClusterClient entry points).
DialTimeout only covered net.Dialer.DialContext. The handshake (HELLO, then AUTH/SELECT) ran on the caller's context, so a node that accepts TCP and never answers HELLO held ClusterClient.Subscribe, ClusterClient.SSubscribe, Client.Subscribe and any Do that needs a new conn until the caller's context ended: for ever under context.Background. handshake now runs under context.WithTimeout(ctx, DialTimeout) (the 5 s default when unset). All three dial paths (unix event loop, net.Conn loop, direct) and so the pool's command and pub/sub dials end in handshake. A caller's shorter deadline still wins and is reported as the caller's; the driver's bound is reported as an error that matches context.DeadlineExceeded and keeps the underlying cause in the chain. The DialTimeout docs (Config, ClusterConfig, SentinelConfig, WithDialTimeout) now say it bounds the connect and, separately, the handshake.
|
Caution Review failedThe pull request is closed.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This was referenced Nov 1, 2026
Merged
…llation; say the timeout once (Refs #1205) Review r4 of #1209: - a client with DialTimeout unset still bounds a silent HELLO node: a test hook lowers the default so the test does not wait 5 s, and the bound is checked to be the default's; - a caller that cancels during the handshake gets context.Canceled back at once, not the driver's bound, and the half-open conn is closed; - the loop paths' error no longer repeats the context error ("handshake timed out after 1s: context deadline exceeded"); the direct path keeps its socket cause in the chain; - the Client and ClusterClient entry-point tests assert the handshake timeout, not any error.
FumingPower3925
marked this pull request as ready for review
November 1, 2026 12:02
This was referenced Nov 1, 2026
Merged
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
driver/redis: bound the connection handshake by DialTimeout. Refs #1205 (found by the review of #1154; the code is older than that PR).
The problem
DialTimeoutonly coverednet.Dialer.DialContext. The handshake (HELLO, thenAUTH/SELECT) ran on the caller's context, so a node that accepts TCP and never answersHELLOheldClusterClient.Subscribe,ClusterClient.SSubscribe,Client.Subscribeand anyDothat needed a new conn until the caller's context ended: for ever undercontext.Background(). (The reconnect loop was not affected: it bounds its own dial with a 5 s context.)The change
redisConn.handshakeruns undercontext.WithTimeout(ctx, DialTimeout)(5 s when unset). All three dial paths end in it (dialRedisConnon unix,dialRedisConnNetConn,dialDirectRedisConn), and the pool's command and pub/sub dials use those, soDo,Pipeline, the cluster node clients and the sentinel clients are bounded the same way. Nothing else indriver/redisruns a handshake (grep 'handshake(' driver/redis: the three callers). Doc ofDialTimeout,WithDialTimeout,ClusterConfig.DialTimeoutandSentinelConfig.DialTimeoutupdated.Commits:
3edf21cthe tests (RED on main),361a5a7the fix.RED / GREEN
RED,
3edf21calone (macOS arm64,go test ./driver/redis -run 1205 -v): the three dial paths (direct,eventloop,netconn),Client.Subscribe,Client.Do,ClusterClient.SubscribeandClusterClient.SSubscribeall reportstill blocked after 2s: the handshake is not bounded by DialTimeout(test code uses a context without deadline and a 2 s watchdog, so main fails with a message, not a 10 min timeout).TestDialHandshakeKeepsTheCallersShorterDeadline1205passes on main by design: it guards that a caller's shorter deadline still wins.GREEN,
361a5a7: all pass; the dials return after 1.00 s (DialTimeout 1 s), the cluster ones after 0.50 s (DialTimeout 500 ms).go test ./driver/redis -race -run 1205 -count=20: ok (macOS arm64, 55 s). The first run of this count=20 found a real flake in my first version (the direct path's socket deadline fires a hair before the context timer, so the "timed out" check saw no context error), fixed: the driver's bound is recognised by the socket deadline too.driver/redispackage:-race -count=1and plain-count=1, Linux (golang:1.27.3,--cpus 2): ok both (race 170 s, plain 174 s);-race -count=1on macOS: ok (38 s).golangci-lintv2.15.0,GOOS=linux: no finding in the files of this PR (twoerrcheckfindings inclose_after_teardown_859_linux_test.go:135andpubsub_rig_engines_887_linux_test.go:77exist on main too and are not touched here).GOOS=windows go vet ./driver/redis: clean.Behaviour notes
ForceRESP2legacy-auth path and theSELECT). A conn is bounded byDialTimeoutfor the connect and, separately, byDialTimeoutfor the handshake: a slow connect followed by a silentHELLOtakes up to about 2 xDialTimeout; the silent-node case in the issue takes about 1 x.celeris-redis: handshake timed out after <d>: context deadline exceeded: <cause>, which matchescontext.DeadlineExceeded(and, on the direct path, the socket'sos.ErrDeadlineExceeded) witherrors.Is.useDirect) honours only a context's deadline, not its cancellation, while it waits for a reply. That is older behaviour and untouched here (the test's watchdog does not wait for a blocked direct dial for that reason).ClusterClient.Subscribeagainst a cluster whose every node is silent now fails after about oneDialTimeoutper node it tries (each node once), instead of never.Release note
DialTimeout(default 5 s) now also bounds the connection handshake (HELLO/AUTH/SELECT, including theForceRESP2path) of every new conn, for commands and pub/sub: a node that accepts the connection and does not answer used to holdSubscribe,SSubscribeandDountil the caller's context ended, for ever undercontext.Background(). A server that takes longer thanDialTimeoutto answer the handshake now fails the dial, also when the caller's context has a longer deadline or none.